package com.yiwei.jmx.constant;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.yiwei.jmx.Application;
import com.yiwei.jmx.api.model.InitAppModel;

import com.yiwei.jmx.api.model.UserModel;
import com.yiwei.jmx.event.LoginSuccessEvent;
import com.yiwei.jmx.event.LogoutEvent;
import com.yiwei.jmx.util.PreferenceUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import retrofit2.http.PUT;


/**
 * Created by xsooy-pc on 18-3-7.
 */

public class UserInfoGlobal {
    public static UserModel user_info;

    public static void saveInitData(InitAppModel.DatasBean item) {
        PreferenceUtils.putString(Application.getInstance(), "is_login", item.is_logined);
        PreferenceUtils.putString(Application.getInstance(), "lastest_version", item.version.lastest_version);
        PreferenceUtils.putString(Application.getInstance(), "download_url", item.version.download_url);
        PreferenceUtils.putString(Application.getInstance(), "upgrade_msg", item.version.upgrade_msg);
        PreferenceUtils.putString(Application.getInstance(), "service_phone", item.contact_info.service_phone);
        PreferenceUtils.putInt(Application.getInstance(), "forced_upgrade", item.version.forced_upgrade);


        PreferenceUtils.putString(Application.getInstance(), "bankcard_no", item.gathering.bankcard_no);
        PreferenceUtils.putString(Application.getInstance(), "name", item.gathering.name);
        PreferenceUtils.putString(Application.getInstance(), "bankzone", item.gathering.bankzone);

        PreferenceUtils.putString(Application.getInstance(), "day_limit", item.bank_limit.day_limit);
        PreferenceUtils.putString(Application.getInstance(), "month_limit", item.bank_limit.month_limit);

    }

    public static void saveUserInfo(UserModel model) {
        user_info = model;
        PreferenceUtils.putString(Application.getInstance(), "mobile", model.mobile);
        PreferenceUtils.putString(Application.getInstance(), "real_name", model.real_name + "");
        PreferenceUtils.putString(Application.getInstance(), "u_alipay", model.u_alipay);
        PreferenceUtils.putString(Application.getInstance(), "address_city", model.provinces_cities + "");
        PreferenceUtils.putString(Application.getInstance(), "idcardpassed", model.idcardpassed);
        PreferenceUtils.putString(Application.getInstance(), "emailpassed", model.emailpassed);
        PreferenceUtils.putString(Application.getInstance(), "email", model.email);
        PreferenceUtils.putString(Application.getInstance(), "header", model.header);
        PreferenceUtils.putString(Application.getInstance(), "set_passwd", model.set_passwd);
        PreferenceUtils.putString(Application.getInstance(), "sex", model.sex + "");
        PreferenceUtils.putString(Application.getInstance(), "username", model.user_name);
        PreferenceUtils.putString(Application.getInstance(), "invite_code", model.invite_code);
        PreferenceUtils.putString(Application.getInstance(), "idno", model.idno);
        PreferenceUtils.putString(Application.getInstance(), "pid", model.pid);

    }

    public static boolean getIsHasInvite() {
        if (PreferenceUtils.getString(Application.getInstance(), "pid", "0").equals("0")) {
            return false;
        } else {
            return true;
        }
    }

    public static void setInvite() {
        PreferenceUtils.putString(Application.getInstance(), "pid","1");
    }

    public static String getLat() {
        return PreferenceUtils.getDouble(Application.getInstance(), Const.KEY.LAT, -1) + "";
    }

    public static String getLng() {
        return PreferenceUtils.getDouble(Application.getInstance(), Const.KEY.LNG, -1) + "";
    }

    public static String getRealName() {
        return PreferenceUtils.getString(Application.getInstance(), "real_name", "");
    }

    public static String getIdNo() {
        return PreferenceUtils.getString(Application.getInstance(), "idno", "");
    }

    public static String getDealId() {
        return PreferenceUtils.getString(Application.getInstance(), "deal_id", "");
    }

    public static void setDealId(String deal_id) {
        PreferenceUtils.putString(Application.getInstance(), "deal_id", deal_id);
    }

    public static String getUserSex() {
        if (PreferenceUtils.getString(Application.getInstance(), "sex", "").equals("1")) {
            return "男";
        } else if (PreferenceUtils.getString(Application.getInstance(), "sex", "").equals("2")) {
            return "女";
        } else return "未设置";
    }

    public static String getMobile() {
        return PreferenceUtils.getString(Application.getInstance(), "mobile", "");
    }

    public static String getSomeMobile() {
        String mobile = PreferenceUtils.getString(Application.getInstance(), "mobile", "");
        if (mobile.length() >= 11) {
            return mobile.substring(0, 3) + "****" + mobile.substring(7, mobile.length());
        } else {
            return "";
        }
    }

    public static String getInviteCode() {
        return PreferenceUtils.getString(Application.getInstance(), "invite_code", "");
    }


    public static String getHeader() {
        return PreferenceUtils.getString(Application.getInstance(), "header", "");
    }

    public static boolean hasUserNick() {
        if (!TextUtils.isEmpty(PreferenceUtils.getString(Application.getInstance(), "username", ""))) {
            return true;
        } else {
            return false;
        }
    }

    public static String getUserNick() {
        if (!TextUtils.isEmpty(PreferenceUtils.getString(Application.getInstance(), "username", ""))) {
            return PreferenceUtils.getString(Application.getInstance(), "username", "");
        } else {
            return getSomeMobile();
        }
    }

    public static void setUserIcon(String icon) {
        PreferenceUtils.putString(Application.getInstance(), "icon", icon);
    }

    public static String getUserIcon() {
        if (!TextUtils.isEmpty(PreferenceUtils.getString(Application.getInstance(), "icon", ""))) {
            return PreferenceUtils.getString(Application.getInstance(), "icon", "");
        } else {
            return "";
        }
    }

    public static boolean getIsIdcardPass() {
        if (PreferenceUtils.getString(Application.getInstance(), "idcardpassed", "0").equals("1")) {
            return true;
        } else {
            return false;
        }
    }

    public static void saveNickName(String name) {
        PreferenceUtils.putString(Application.getInstance(), "username", name);
    }

    public static void saveUserSex(String sex) {
        PreferenceUtils.putString(Application.getInstance(), "sex", sex);
    }

    public static void setIsOpenGuesture(boolean isOpenGuesture) {
        PreferenceUtils.putBoolean(Application.getInstance(), "is_open_guesture", isOpenGuesture);
    }

    public static void setIsOpenGuestureRoot(boolean isGestrueRoot) {
        PreferenceUtils.putBoolean(Application.getInstance(), "is_open_guesture_root", isGestrueRoot);
    }

    public static boolean getIsOpenGuestureRoot() {
        return PreferenceUtils.getBoolean(Application.getInstance(), "is_open_guesture_root", true);
    }

    public static boolean getIsOpenGuesture() {
        return PreferenceUtils.getBoolean(Application.getInstance(), "is_open_guesture", false);
    }

    public static boolean isLogin() {
        if (PreferenceUtils.getString(Application.getInstance(), "is_login", "0").equals("1")) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean checkLogin() {

        if (PreferenceUtils.getString(Application.getInstance(), "is_login", "0").equals("1")) {

            return true;
        }
        return false;
    }

    public static void login() {
        PreferenceUtils.putString(Application.getInstance(), "is_login", "1");
        EventBus.getDefault().post(new LoginSuccessEvent());
        saveGoLogoutTime();
    }

    public static void clearUserInfo(Context context) {
        PreferenceUtils.remove(context, "token");
        PreferenceUtils.remove(context, "username");
        PreferenceUtils.remove(context, "mobile");
        PreferenceUtils.remove(context, "money");
        PreferenceUtils.remove(context, "is_logined");
        PreferenceUtils.remove(context, "has_user_name");
        PreferenceUtils.remove(context, "imposeDay");
        PreferenceUtils.remove(context, "hasRepay");
        PreferenceUtils.remove(context, "repayMoney");
        PreferenceUtils.remove(context, "email");
        PreferenceUtils.remove(context, "header");
        PreferenceUtils.remove(context, "find_xuexin_username");

    }

    public static void logout() {
        clearUserInfo(Application.getInstance());
        PreferenceUtils.putString(Application.getInstance(), "token", "");
        PreferenceUtils.putString(Application.getInstance(), "is_login", "");
        EventBus.getDefault().post(new LogoutEvent());
    }


    public static void saveGoLogoutTime() {
        PreferenceUtils.putLong(Application.getInstance(), "go_logout_time", System.currentTimeMillis());
    }

    public static boolean isNeedLogout() {
        if (System.currentTimeMillis() - PreferenceUtils.getLong(Application.getInstance(), "go_logout_time", System.currentTimeMillis()) > wait_time) {
            return true;
        } else {
            PreferenceUtils.putLong(Application.getInstance(), "go_logout_time", System.currentTimeMillis());
            return false;
        }
    }


    public static long wait_time = 1000 * 60 * 120;
}
